<template>
  <main-page>
    <div slot="body">
      <el-form :inline="true" :model="search" size="mini">
        <el-form-item>
          <el-input v-model="search.booksName" placeholder="小说名称" autocomplete="off" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="search.readerSex"
            style="width:100%"
            placeholder="适合读者"
            clearable
            @change="readerSexChange"
          >
            <el-option :key="config.blockType.man" label="男生" :value="config.blockType.man"></el-option>
            <el-option :key="config.blockType.woman" label="女生" :value="config.blockType.woman"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="search.sortId" style="width:100%" placeholder="小说分类" clearable>
            <el-option
              v-for="item in sortList"
              :key="item.id"
              :label="item.sortName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="search.isOver" style="width:100%" placeholder="是否完结" clearable>
            <el-option :key="config.isOver.Over" label="已完结" :value="config.isOver.Over"></el-option>
            <el-option :key="config.isOver.No_Over" label="连载中" :value="config.isOver.No_Over"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getBooksList">搜索</el-button>
          <el-button type="danger" @click="dialogFormVisible = true;">新增</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%" tooltip-effect="dark">
        <el-table-column prop="id" label="编号" width="80"></el-table-column>
        <el-table-column prop="booksName" label="小说名称"></el-table-column>
        <el-table-column prop="author" label="作者"></el-table-column>
        <el-table-column label="适合读者">
          <template slot-scope="scope">
            <el-tag type="success">{{scope.row.readerSex == config.blockType.man ? '男生' : '女生'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="封面">
          <template slot-scope="scope">
            <el-image style="width: 60px;" :src="scope.row.booksCover" fit="contain"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="分类">
          <template slot-scope="scope">
            <el-tag
              type="info"
            >{{scope.row.parentSortVo != undefined ? scope.row.parentSortVo.sortName : ''}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="子分类">
          <template slot-scope="scope">
            <template v-if="scope.row.sortVoList != undefined">
              <el-tag
                type="info"
                v-for="(sort, index) in scope.row.sortVoList"
                :key="index"
              >{{sort.sortName}}</el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              title="编辑"
              size="small"
              @click="handleClick2EditById(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              title="删除"
              size="small"
              @click="handleClick2DeleteById(scope.row.id)"
            ></el-button>
            <el-button
              type="info"
              icon="iconfont icon-fabu"
              circle
              title="设置子分类"
              size="small"
              @click="handleClick2SetSort(scope.row)"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-arrow-right"
              circle
              title="查看小说章节"
              size="small"
              @click="$router.push('/list/'+scope.row.id+'/chapter')"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination :listNum="listNum" @currentChange="handleCurrentChange"></pagination>

      <el-dialog title="小说管理" :visible.sync="dialogFormVisible" width="40%">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="demo-ruleForm">
          <el-form-item label="小说名称" prop="booksName">
            <el-input v-model="form.booksName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="作者" prop="author">
            <el-input v-model="form.author" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="读者" prop="readerSex">
            <el-radio-group v-model="form.readerSex" @change="readerSexChange">
              <el-radio :label="config.blockType.man">男生</el-radio>
              <el-radio :label="config.blockType.woman">女生</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="小说分类" prop="sortId">
            <el-select v-model="form.sortId" style="width:100%" placeholder="小说分类" clearable>
              <el-option
                v-for="item in sortList"
                :key="item.id"
                :label="item.sortName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="小说简介" prop="booksDescription">
            <el-input
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4}"
              v-model="form.booksDescription"
            ></el-input>
          </el-form-item>
          <el-form-item label="小说封面" prop="booksCover">
            <!-- list-type="picture-card" -->
            <el-upload
              class="avatar-uploader"
              :action="rootApi"
              :on-remove="handleRemove"
              :on-success="handleAvatarSuccess"
              :before-upload="handleBeforeUpload"
              :limit="1"
            >
              <img v-if="form.booksCover" :src="form.booksCover" class="avatar" />
              <i class="el-icon-plus avatar-uploader-icon" v-else></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="完结状态" prop="isOver">
            <el-radio-group v-model="form.isOver">
              <el-radio :label="config.isOver.Over">已完结</el-radio>
              <el-radio :label="config.isOver.No_Over">连载中</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="onSubmit">提交</el-button>
        </div>
      </el-dialog>

      <el-dialog title="设置分类" :visible.sync="dialogSortFormVisible" width="40%">
        <el-form :model="sortForm" label-width="80px">
          <el-form-item label="小说分类">
            <el-select v-model="sortForm.sortIds" multiple placeholder="请选择" style="width: 100%">
              <el-option
                v-for="sort in childSortList"
                :key="sort.id"
                :label="sort.sortName"
                :value="sort.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="onSortFormSubmit">提交</el-button>
        </div>
      </el-dialog>
    </div>
  </main-page>
</template>

<script>
import {
  getBooksList,
  saveBooks,
  deleteBooks,
  updateBooksById
} from "api/BooksApi";

import { getSortIdByBooksId, saveSortJoinBooks } from "api/SortJoinBooksApi";

import { getSortAllList } from "api/SortApi";
import { deleteFileByUrl } from "api/FileApi";

import { config } from "assets/js/Data";

export default {
  name: "BooksList",
  data() {
    return {
      rootApi: process.env.VUE_APP_UPLOAD_URL,
      tableData: [],
      listNum: 0,
      search: {
        currentIndex: 1,
        booksName: "",
        sortId: "",
        isOver: "",
        readerSex: ""
      },
      dialogFormVisible: false,
      dialogVisible: false,
      dialogImageUrl: "",
      form: {
        id: "",
        booksName: "",
        author: "",
        sortId: "",
        isOver: "",
        booksCover: "",
        booksDescription: "",
        readerSex: ""
      },
      rules: {
        booksName: [{ required: true, message: "请输入小说名称" }],
        author: [{ required: true, message: "请输入小说作者" }],
        sortId: [{ required: true, message: "请选择小说分类" }],
        booksCover: [{ required: true, message: "请上传小说封面" }],
        booksDescription: [{ required: true, message: "请输入小说简介" }],
        readerSex: [{ required: true, message: "请选择读者" }],
        isOver: [{ required: true, message: "请选择小说完结状态" }]
      },
      sortList: [],
      childSortList: [],
      config: config,
      dialogSortFormVisible: false,
      sortForm: {
        sortIds: [],
        booksId: ""
      }
    };
  },
  methods: {
    getBooksList() {
      getBooksList(this.search).then(res => {
        this.tableData = res.datas;
        this.listNum = res.pagination.totalCount;
      });
    },
    handleClick2SetSort(row) {
      //设置分类
      this.childSortList = []
      this.dialogSortFormVisible = true;
      this.sortForm.booksId = row.id;

      //查询数据
      getSortAllList({
        parentId: row.sortId
      }).then(res => {
        if (res) {
          this.childSortList = res;
        }
      });

      //查询当前小说设置的子分类
      getSortIdByBooksId({
        booksId: row.id
      }).then(res => {
        if (res) {
          this.sortForm.sortIds = res;
        }
      });
    },
    getSortAllList() {
      getSortAllList({
        parentId: 0,
        blockId: this.search.readerSex
      }).then(res => {
        if (res) {
          this.sortList = res;
        }
      });
    },
    handleClick2EditById(row) {
      this.dialogFormVisible = true;
      this.$base.set2Form(this.form, row);
      this.dialogImageUrl = this.form.booksCover;
    },
    handleClick2DeleteById(id) {
      this.$base.deleteConfirm(() => {
        deleteBooks({ id }).then(res => {
          if (res) {
            this.$base.deleteOk();
            this.getBooksList();
          }
        });
      });
    },
    handleCurrentChange(page) {
      this.search.currentIndex = page;
      this.getBooksList();
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id) {
            //修改
            updateBooksById(this.form).then(res => {
              if (res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getBooksList();
              }
            });
          } else {
            //保存
            saveBooks(this.form).then(res => {
              if (res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getBooksList();
              }
            });
          }
        }
      });
    },
    onSortFormSubmit() {
      //设置子分类
      saveSortJoinBooks(this.sortForm).then(res => {
        if (res) {
          this.$base.showOk("小说分类设置完成");
          this.getBooksList()
        }
      });
    },
    handleRemove(file, fileList) {
      this.deleteFileByUrl();
    },
    deleteFileByUrl() {
      deleteFileByUrl({
        key: this.form.booksCover,
        module: config.module.UTILS
      }).then(res => {
        if (res) {
        }
      });
    },
    handleAvatarSuccess(res, file) {
      if (res.meta.success) {
        this.form.booksCover = res.data[0];
      }
    },
    handleBeforeUpload(file) {
      this.deleteFileByUrl();
    },
    readerSexChange(label) {
      this.search.readerSex = label;
      this.getSortAllList();
    }
  },
  created() {
    this.getBooksList();
    this.getSortAllList();
  },
  watch: {
    dialogFormVisible(newDialogFormVisible) {
      if (!newDialogFormVisible) {
        this.$base.clearObj(this.form);
        this.search.readerSex = "";

        this.$refs.form.resetFields();
      } else {
        this.getSortAllList();
      }
    },
    dialogSortFormVisible(newDialogSortFormVisible) {
      if (!newDialogSortFormVisible) {
        this.$base.clearObj(this.sortForm);
      }
    }
  }
};
</script>
<style lang="stylus" scoped>
  .avatar {
    height auto
  }
</style>
